//
//  MainGameViewController.m
//  FINALPROJECTBOWANDARROW
//
//  Created by iD Student on 7/29/13.
//  Copyright (c) 2013 idstudent. All rights reserved.
//

#import "MainGameViewController.h"
#import <math.h>

@interface MainGameViewController ()

@end

@implementation MainGameViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadLevel:3];
    
}

-(void)loadArrow
{
    //Load image to arrow
    //Place arrow on top of bow in bottom right corner
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)gameUpdate
{

    if (CGRectIntersectsRect(movingImage.frame, target.frame) || movetarget)
    {
        
        CGPoint oldCenter = target.center;
        target.center = CGPointMake (oldCenter.x-vLeft, oldCenter.y - vUp);
        movearrow = NO;
        movetarget = YES;
    }
    
    if (movearrow == YES)
    {
    CGPoint oldCenter = movingImage.center;
        movingImage.center = CGPointMake(oldCenter.x-vLeft, oldCenter.y - vUp);
        
    }
}

- (IBAction)bowtap:(id)sender
{
    myangle = [angle value];
    mypower = [power value]/10;
    
    ratio = 1 / tan(myangle * (M_PI / 180));
    
    vUp = 1*mypower;
    vLeft = ratio*mypower;
    
    NSLog(@"Bow was tapped");
    movearrow = YES;
    arrowTimer = [NSTimer scheduledTimerWithTimeInterval:.004 target:self selector:@selector(gameUpdate) userInfo:nil repeats:YES];
    
}

- (IBAction)fire:(id)sender {
    [self bowtap:sender];
}

- (IBAction)reset:(id)sender
{
    [arrowTimer invalidate];
    arrowTimer= nil;
    vUp=0;
    vLeft=0;
    movetarget=0;
    movearrow=0;
    mypower=0;
    myangle= 0;
    movingImage.center = CGPointMake(404,228);
    //target.center = CGPointMake(90,84);
}

-(void)loadLevel:(int)levelID
{
    
    [self reset:nil];
    
    switch (levelID){
        case 1:
            target.center = CGPointMake(90,84);
            //[levelImage setImage:[UIImage imageNamed:@""]];
            break;
        case 2:
            target.center = CGPointMake(105 ,105);
            //[levelImage setImage:[UIImage imageNamed:@""]];
            break;
        case 3:
            target.center = CGPointMake(0,0);
            //[levelImage setImage:[UIImage imageNamed:@""]];
            break;
    }
    
}



@end
